package com.jiulong.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

	public class WordTest {  
	      
	    private Configuration configuration = null;  
	      
	    public WordTest(){  
	        configuration = new Configuration();  
	        configuration.setDefaultEncoding("UTF-8");  
	    }  
	      
	    public static void main(String[] args) throws IOException {  
	        WordTest test = new WordTest();  
	        test.createWord();  
	    }  
	      
	    public void createWord() throws IOException{  
	        Map<String,Object> dataMap=new HashMap<String,Object>();  
	        getData(dataMap);  
	        //configuration.setClassForTemplateLoading(this.getClass(), "E:\\");  //FTL文件所存在的位置  
	        configuration.setDirectoryForTemplateLoading(new File("E:\\")); //FTL文件所存在的位置
	        	Template t=null;  
	        try {  
	            t = configuration.getTemplate("waitCheckd.ftl"); //文件名  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        File outFile = new File("E:/outFilessa"+Math.random()*10000+".xls");  //生成文件的路径
	        Writer out = null;  
	        try {  
	            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));  
	        } catch (FileNotFoundException e1) {  
	            e1.printStackTrace();  
	        }  
	           
	        try {  
	            t.process(dataMap, out);  
	        } catch (TemplateException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	    //这里赋值的时候需要注意,xml中需要的数据你必须提供给它,不然会报找不到某元素错的.
	    private void getData(Map<String, Object> dataMap) {  
	        dataMap.put("title", "标题");  
	        dataMap.put("year", "2012");  
	        dataMap.put("month", "2");  
	        dataMap.put("day", "13");  
	        dataMap.put("auditor", "鑫");  
	        dataMap.put("phone", "xxxxxxxxxxxxx");  
	        dataMap.put("weave", "文涛");  
//	      dataMap.put("number", 1);  
//	      dataMap.put("content", "内容"+2);  
	          
	        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
	        for (int i = 0; i < 10; i++) {  
	            Map<String,Object> map = new HashMap<String,Object>();  
	            map.put("number", i);  
	            map.put("content", "内容"+i);  
	            list.add(map);  
	        }  
	          
	          
	        dataMap.put("list", list);  
	    }  
	}  